What is @lukeed/ms?
@lukeed/ms is a lightweight utility for converting milliseconds to human-readable strings and vice versa. It is useful for handling time durations in a more readable format.
What are @lukeed/ms's main functionalities?
Convert milliseconds to human-readable string
This feature allows you to convert a duration in milliseconds to a human-readable string format. For example, 1500 milliseconds is converted to '1.5s'.
const ms = require('@lukeed/ms');
console.log(ms(1500)); // '1.5s'
Convert human-readable string to milliseconds
This feature allows you to convert a human-readable string representing a duration back to milliseconds. For example, '1.5s' is converted to 1500 milliseconds.
const ms = require('@lukeed/ms');
console.log(ms('1.5s')); // 1500
Other packages similar to @lukeed/ms
ms
The 'ms' package provides similar functionality to @lukeed/ms, allowing conversion between milliseconds and human-readable strings. It is widely used and has a similar API, making it a good alternative.
pretty-ms
The 'pretty-ms' package focuses on converting milliseconds to human-readable strings with more customization options. It offers more formatting options compared to @lukeed/ms, making it suitable for more complex use cases.
ms
A tiny (414B) and fast utility to convert milliseconds to and from strings.
NOTICE: This is a fork of vercel/ms!
In June 2019, I opened a PR with signficiant performance and code size improvements. After nearly 2 years of silence, it was eventually closed. :cry: A year into my wait, I started anew (this repo), hoping to improve upon my own improvements.
This module is delivered as:
Install
$ npm install --save @lukeed/ms
Usage
import { parse, format } from '@lukeed/ms';
parse('2 days');
parse('1d');
parse('10h');
parse('2.5 hrs');
parse('2h');
parse('1m');
parse('5s');
parse('1y');
parse('100');
parse('-3 days');
parse('-1h');
parse('-200');
format(60000);
format(2 * 60000);
format(-3 * 60000);
format(parse('10 hours'));
format(60000, true);
format(2 * 60000, true);
format(-3 * 60000, true);
format(parse('10 hours'), true);
API
ms.parse(input)
Returns: Number
Parses the input string, returning the number of milliseconds.
input
Type: String
The human-readable time string; eg: 10min
, 10m
, 10 minutes
.
ms.format(milli, long?)
Returns: Number
Formats the millisecond count to a human-readable time string.
Important: The output will be rounded to the nearest whole integer.
milli
Type: Number
The number of milliseconds.
long
Type: Boolean
Default: false
Whether or not the output should use the interval's long/full form; eg hour
or hours
instead of h
.
Note: When long
, the count and interval will be separated by a single space.
Also, when long
, the interval may be pluralized; eg 1 second
vs 2 seconds
.
Benchmarks
Running on Node.js v12.18.4
Validation :: parse
✔ lukeed/ms
✔ zeit/ms
Benchmark :: "parse"
lukeed/ms x 351,319 ops/sec ±0.31% (96 runs sampled)
zeit/ms x 245,576 ops/sec ±1.66% (94 runs sampled)
Benchmark :: "parse" (long)
lukeed/ms x 335,538 ops/sec ±0.50% (94 runs sampled)
zeit/ms x 265,410 ops/sec ±1.72% (95 runs sampled)
Validation :: format
✔ lukeed/ms
✔ zeit/ms
Benchmark :: "format"
lukeed/ms x 4,109,440 ops/sec ±0.35% (94 runs sampled)
zeit/ms x 3,420,198 ops/sec ±1.61% (94 runs sampled)
Benchmark :: "format" (long)
lukeed/ms x 3,402,872 ops/sec ±0.14% (97 runs sampled)
zeit/ms x 1,344,908 ops/sec ±3.68% (96 runs sampled)
Credits
This is obviously a fork of zeit/ms.
I opened a PR in June 2019 that introduced significant performance gains and code reduction — it was ignored for nearly two years. This repository is a from-scratch re-implementation that takes the goals of that PR a bit further.
License
MIT © Luke Edwards